package hu.fic.weddingstorefinder.db.table;


import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;

import hu.fic.weddingstorefinder.db.generatedtable.GeneratedApplicationConf;


public class ApplicationConf extends GeneratedApplicationConf {
	private static final String LOG_TAG = "AppConf";
	public static final String APPCONF_KEY_LASTUPDATE_TIMESTAMP = "LastUpdateTimestamp";
	

	
	private static HashMap<String, String> hashMap;
	private static ApplicationConf appConf;
	
	public ApplicationConf(SQLiteDatabase db, Activity activity) {
		super(db, activity);
	}
	
	public static void initAppConf(SQLiteDatabase db, Activity activity) throws Exception {
		appConf = new ApplicationConf(db, activity);
		ApplicationConf.initHashMap();

		appConf.clear();
		appConf.loadAll();
		
		while (appConf.nextRow()) {
			hashMap.put(appConf.getName(), appConf.getValue());
		}
	}

	/**
	 * 
	 */
	private static void initHashMap() throws Exception{
		// TODO Auto-generated method stub
		hashMap = new HashMap<String, String>();
		
		hashMap.put(APPCONF_KEY_LASTUPDATE_TIMESTAMP, "0");


	}
	
	public static void saveAppConf() throws Exception{
		appConf.deleteAllRows();
		
		Set<String> keyset = hashMap.keySet();
		for (String key : keyset) {
			appConf.clear();
			appConf.setName(key);
			appConf.setValue(hashMap.get(key));
			appConf.newRow();
		}
	}
	
	
	//appconf getters and setters:
	
	public static String getStringForKey(String key) {
		if (hashMap.get(key) != null) {
			return hashMap.get(key);
		} else {
			return "";
		}
		
	}
	
	public static void setStringForKey(String key, String value) {
		hashMap.put(key, value);
	}
	
	public static int getIntForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Integer(hashMap.get(key)).intValue();
		} else {
			return -1;
		}
	}
	
	public static void setIntForKey(String key, int value) {
		hashMap.put(key, value + "");
	}
	
	public static float getFloatForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Float(hashMap.get(key)).floatValue();
		} else {
			return -1;
		}
	}
	
	public static void setFloatForKey(String key, float value) {
		hashMap.put(key, value + "");
	}
	
	public static double getDoubleForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Double(hashMap.get(key)).doubleValue();
		} else {
			return -1;
		}
	}
	
	public static void setDoubleForKey(String key, double value) {
		hashMap.put(key, value + "");
	}
	
	public static long getLongForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Long(hashMap.get(key)).longValue();
		} else {
			return -1;
		}
	}
	
	public static void setLongForKey(String key, long value) {
		hashMap.put(key, value + "");
	}
	
	public static boolean getBoolForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Boolean(hashMap.get(key)).booleanValue();
		} else {
			return false;
		}
	}
	
	public static void setBoolForKey(String key, boolean value) {
		hashMap.put(key, value + "");
	}
	
	public static Date getDateForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Date(new Long(hashMap.get(key)).longValue());
		} else {
			return new Date(0);
		}
	}
	
	public static long getTimeStampForKey(String key) {
		if (hashMap.get(key) != null) {
			return new Long(hashMap.get(key)).longValue();
		} else {
			return -1;
		}
	}
	
	public static void setDateForKey(String key, Date value) {
		hashMap.put(key, value.getTime() + "");
	}
	
	
}